﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Sector7.Framework.Core;
using Sector7.Framework.Enums;
using Sector7.Medical.Biz;
using Sector7.Medical.EntityType;
using Sector7.Medical.Events;
using Sector7.Medical.Enums;

namespace Sector7.Medical.Controls
{
    /// <summary>
    /// ReadCardControl.xaml 的交互逻辑
    /// </summary>
    public partial class ReadCardControl : UserControl
    {
        #region Accessor

        /// <summary>
        /// 编辑完成事件
        /// </summary>
        public event PatientLoadedEventHandler PatientLoaded = null;

        #endregion

        #region Public functions

        /// <summary>
        /// 重置控件
        /// </summary>
        public void ResetReadCardControl()
        {
            txbHealthCardId.Text = "";
        }

        #endregion

        #region Private functions

        /// <summary>
        /// Form Loaded
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            ResetReadCardControl();
        }

        /// <summary>
        /// 键盘事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        //private void UserControl_KeyDown(object sender, KeyEventArgs e)
        //{
        //    if (e.Key == Key.Enter)
        //    {
        //        if (e.OriginalSource is TextBox && (e.OriginalSource as TextBox).Tag != null && (e.OriginalSource as TextBox).Tag.ToString() == "HealthIdCard")
        //        {
        //            string healthCardId = txbHealthCardId.Text.Trim();
        //            if (!string.IsNullOrEmpty(healthCardId))
        //            {
        //                GetPatient(healthCardId);
        //            }
        //        }
        //    }
        //}

        /// <summary>
        /// 键盘事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txbHealthCardId_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                string healthCardId = txbHealthCardId.Text.Trim();
                if (!string.IsNullOrEmpty(healthCardId))
                {
                    GetPatient(healthCardId);
                }
            }
        }

        /// <summary>
        /// 获取病人信息
        /// </summary>
        /// <param name="healthCardId"></param>
        private void GetPatient(string healthCardId)
        {
            M7_Patient patient = Agent_BIZS.GetPatientByRegistryNo(RegistryNoType.HealthCardId, healthCardId);

            bool isSuccess = true;
            if (patient == null)
            {
                isSuccess = false;
                Alert.ShowDialog(AlertType.Error, "{0}号码无相应记录，请核对后重新输入", healthCardId);
                ResetReadCardControl();
            }

            PatientLoadedEventArgs args = new PatientLoadedEventArgs(isSuccess, patient);
            OnPatientLoaded(args);
        }

        /// <summary>
        /// 加载病人完成
        /// </summary>
        /// <param name="args"></param>
        private void OnPatientLoaded(PatientLoadedEventArgs args)
        {
            if (PatientLoaded != null)
            {
                PatientLoaded(this, args);
            }
        }
        #endregion

        #region Structure

        /// <summary>
        /// 构造方法
        /// </summary>
        public ReadCardControl()
        {
            InitializeComponent();
        }

        #endregion

        #region Inner members


        #endregion
    }
}
